﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NHibernate;
using NHibernate.Criterion;

namespace RMD.Modal
{
    public class DBCategoryRepository : ICategoryRepository
    {

        #region ICategoryRepository Members

        public IList<Category> GetCategories()
        {
            ISession session = NHHelper.GetNHSession();
            ICriteria crit = session.CreateCriteria<Category>();
            return crit.List<Category>();
        }

        public void AddCategory(Category category)
        {
            ISession session = NHHelper.GetNHSession();
            session.Save(category);
            session.Flush();
            session.Close();
        }

        public IList<Category> SearchCategoryByName(string name)
        {
            ISession session = NHHelper.GetNHSession();
            ICriteria crit = session.CreateCriteria<Category>();
            crit.Add(Expression.Like("FullName", name, MatchMode.Anywhere));
            return crit.List<Category>();
        }

        public IList<Category> SearchCategoryByNumber(string number)
        {
            ISession session = NHHelper.GetNHSession();
            ICriteria crit = session.CreateCriteria<Category>();
            crit.Add(Expression.Like("Number", number, MatchMode.Start));
            return crit.List<Category>();
        }
        #endregion
    }
}
